<template>
  <!-- 新增仓库 -->
  <div class="stock">
    <div class="dropdown">
      <el-button type="primary" size="mini" @click.native="subMarker">{{ $t('btn.save') }}</el-button>
    </div>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" label-width="100px">
          <el-form-item :label="$t('market.stockname')">
            <el-input v-model.trim="prodValue.fname" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.stockNumber')">
            <el-input v-model.trim="prodValue.fnumber" disabled size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.stockType')" prop="fpaezCombo">
            <el-select v-model.trim="prodValue.ftype" size="mini" filterable>
              <el-option
                v-for="(item, index) in billtypes"
                :key="index"
                :label="item.fname"
                :value="item.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.stockgroup')">
            <el-input v-model.trim="prodValue.fstockgroup" size="mini">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="stockVisiblit=true"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.address')">
            <el-input v-model.trim="prodValue.faddress" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.stocker')">
            <el-autocomplete
              v-model.trim="prodValue.fstockerId"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch3"
              size="mini"
              @focus="querySalesperson(3)"
              @select="marketData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="marketVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fuserName }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.descrip')" style="width: 33.2%">
            <el-input v-model.trim="prodValue.fdescripTion" size="mini" style="height: 35px" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <!--仓管员列表-->
    <market v-if="marketVisiblit" :staff-type="staffType" @market="marketData" />
    <stockTree v-if="stockVisiblit" @stock="treeData" />
  </div>
</template>

<script>
import {
  insertStock
} from '@/api/repertory/stock'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'

export default {
  components: {
    stockTree: () => import('@/views/repertory/stock/components/stockTree'),
    market: () => import('@/views/market/marketManage/createMarkerOrder/components/market')
  },
  mixins: [autocomplete],
  data() {
    return {
      staffType: 3, // 仓管员
      activeName: 'first',
      marketVisiblit: false,
      stockVisiblit: false,
      billtypes: [], // 类型
      prodValue: {
        faddress: '',
        ftype: 170,
        fstockerId: 0,
        fstockerName: '',
        fdescripTion: '',
        fstockgroup: ''
      }
    }
  },
  created() {
    this.queryTBasBilltype()
  },
  methods: {
    // 保存
    subMarker() {
      const DATA = {
        ...this.prodValue
      }
      insertStock(DATA).then(res => {
        if (res.code === 0) {
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: `/editStock/${res.data.fstockId}` })
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 查询单据类型
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_STOCKTYPE' })
      this.billtypes = RES.list
    },
    // 获取仓管员数据(子传父)
    marketData(item) {
      if (item.fuserId) {
        this.prodValue.fstockerName = item.fuserName
        this.prodValue.fstockerId = item.fuserId
      }
      this.marketVisiblit = false
    },
    treeData(item) {
      if (item.fid) {
        this.prodValue.fstockgroup = item.fname
        this.prodValue.fstockgroupid = item.fid
      }
      this.stockVisiblit = false
    }
  }
}
</script>

<style scoped lang="scss">
.stock {
  padding: 10px;
  background: #fff;
  min-height: calc(100vh - 100px);
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 350px;
      margin-bottom: 5px;
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
